/** \file ses_manager.c This file contains definitions of variables and functions of the session manager module.
*/
#include "ses_manager.h"


/****************/
/*	Local Types */
/****************/


/*********************/
/*	Local Dfinitions */
/*********************/
#define SES_u8COMMAND_RANGE_START       (u8)0x80
#define SES_u8START_SESSION             (u8)0x81
#define SES_u8END_SESSION               (u8)0x82
#define SES_u8START_MESSAGE             (u8)0x83
#define SES_u8END_MESSAGE               (u8)0x84
#define SES_u8SET_CURRENT               (u8)0x85



#define SES_u8COMMAND_POSITION          (u8)3
#define SES_u8DATALENGTH_POSITION       (u8)4
#define SES_u8DATA_POSITION             (u8)5

#define SES_u8RX_START                  (u8)1
#define SES_u8BUFFER_READY              (u8)2
#define SES_u8WAIT_WRITE_END            (u8)3

#define SES_u8CHAR_ZERO_OFFSET          (u8)48

#define SES_nPACKET_NUMBER_INDEX		3
#define SES_u8CHAR_ZERO_OFFSET			(u8)48

/*******************/
/*	Local Variables*/
/*******************/
static u8 u8SessionState;

static u8 u8PrevSessionState;



static u8 u8RxMessageState;

static TYP_tu8Bool bWriteEnded;

static u8 u8DataLength;

static u8 u8CurrentMessageDisplay;

static BUF_tstrBuffer *pstrDataFrame;

static u8 au8MessageTransferBuffer[16] ;


static u8 au8SessionStarted[]   	= "Ses Start";
static u8 au8SessionEnded[]     	= "Ses End";
static u8 au8MessageStarted[]   	= "Msg Start";
static u8 au8MessageEnded[]     	= "Msg end";
static u8 au8CurrentMessageSet[]    = "Msg Set";
static u8 au8MessageError[]    		= "Msg Err";
static u8 au8PacketReceived[]   	= "Rx 0";

/*********************************/
/*	Local Functions' Declaration */
/*********************************/


/***********************************/
/*	External Functions' Definition */
/***********************************/
void SES_vidInit(void)
{
	u8SessionState				= SES_u8SESSION_IDLE;
	u8PrevSessionState			= SES_u8SESSION_IDLE;
	u8RxMessageState			= SES_u8RX_START;
	bWriteEnded					= TYP_u8FALSE;
	u8DataLength				= TYP_u8ZERO;
	u8CurrentMessageDisplay		= TYP_u8ZERO;
	pstrDataFrame				= (BUF_tstrBuffer *)0;

}


void SES_vidMainFunction(void)
{

    u8 u8Indexer;

    u8 u8Temp;

    u8 u8FrameReturn;

    u8 u8Command;

    switch (u8SessionState)
    {
        case SES_u8SESSION_IDLE:
                
                    
            u8FrameReturn = COM_u8ParseReceivedFrame();
            
            if (u8FrameReturn == COM_u8FRAME_STARTED)
            {
                u8SessionState = SES_u8SESSION_FRAME;
                u8PrevSessionState = SES_u8SESSION_IDLE;
            }

            break;

        case SES_u8SESSION_FRAME:

            SES_vidINCREASE_PERIOD();
            u8SessionState = SES_u8SESSION_START;
            u8PrevSessionState = SES_u8SESSION_FRAME;
            break;

        case SES_u8SESSION_START:
            
            u8FrameReturn = COM_u8ParseReceivedFrame();
            
            if (u8FrameReturn == COM_u8FRAME_ENDED)
            {
                u8Command = BUF_u8GetAtIndex(pstrDataFrame, SES_u8COMMAND_POSITION);

                if (u8Command == SES_u8START_SESSION)
                {
                    u8Temp = SES_bSEND_STRING(au8SessionStarted);                    

                    if (u8Temp == TYP_u8TRUE)
                    {
                        BUF_vidFlush(pstrDataFrame);
                        u8SessionState = SES_u8SESSION_RUN;
                        u8PrevSessionState = SES_u8SESSION_START;
                    }
                }
                else 
                {
                    
                    u8SessionState = SES_u8SESSION_ERROR;
                    u8PrevSessionState = SES_u8SESSION_START;
                }
            }
            else if (u8FrameReturn == COM_u8FRAME_ERROR)
            {
                
                u8SessionState = SES_u8SESSION_ERROR;
                u8PrevSessionState = SES_u8SESSION_START;
            }

            break;

        case SES_u8SESSION_RUN:

            u8FrameReturn = COM_u8ParseReceivedFrame();
            
            if (u8FrameReturn == COM_u8FRAME_ENDED)
            {
                
                u8Command = BUF_u8GetAtIndex(pstrDataFrame, SES_u8COMMAND_POSITION);

                if (u8Command == SES_u8START_MESSAGE)
                {
					u8Temp = SES_bSEND_STRING(au8MessageStarted);                    

                    if (u8Temp == TYP_u8TRUE)
                    {
                    	MSG_vidStartSavingMessages();
                    	BUF_vidFlush(pstrDataFrame);
                    	u8SessionState      = SES_u8SESSION_RX_MESSAGE;
                    	u8PrevSessionState  = SES_u8SESSION_RUN;
					}
                }

                else if (u8Command ==  SES_u8END_SESSION)
                {
                    u8Temp = SES_bSEND_STRING(au8SessionEnded);                    

                    if (u8Temp == TYP_u8TRUE)
                    {
						MSG_vidUpdateMessage(u8CurrentMessageDisplay);
                    	BUF_vidFlush(pstrDataFrame);
                    	u8SessionState      = SES_u8SESSION_IDLE;
                    	u8PrevSessionState  = SES_u8SESSION_RUN;
					}
                }
                else if (u8Command ==  SES_u8SET_CURRENT)
                {
                    u8SessionState      = SES_u8SESSION_SET_CURRENT;
                    u8PrevSessionState  = SES_u8SESSION_RUN;
                }
                else
                {
                    
                    u8SessionState = SES_u8SESSION_ERROR;
                    u8PrevSessionState = SES_u8SESSION_START;
                }
            }
            else if (u8FrameReturn == COM_u8FRAME_ERROR)
            {
                
                u8SessionState = SES_u8SESSION_ERROR;
                u8PrevSessionState = SES_u8SESSION_START;
            }

            break;
        case SES_u8SESSION_RX_MESSAGE:

            if (u8RxMessageState == SES_u8RX_START)
            {
                u8FrameReturn = COM_u8ParseReceivedFrame();
            
                if (u8FrameReturn == COM_u8FRAME_ENDED)
                {
                    u8Command = BUF_u8GetAtIndex(pstrDataFrame, SES_u8COMMAND_POSITION);
                    
                    u8DataLength = BUF_u8GetAtIndex(pstrDataFrame, SES_u8DATALENGTH_POSITION);
                    
                    if (u8Command < SES_u8COMMAND_RANGE_START)
                    {
                        for ( u8Indexer = TYP_u8ZERO; u8Indexer<u8DataLength;u8Indexer++)
                        {
                            au8MessageTransferBuffer[u8Indexer] = BUF_u8GetAtIndex(pstrDataFrame, SES_u8DATA_POSITION + u8Indexer);
                        }

                        u8RxMessageState = SES_u8BUFFER_READY;
                    }
                    else if (u8Command == SES_u8END_MESSAGE)
                    {
                         
                        u8Temp = BUF_u8GetAtIndex(pstrDataFrame, SES_u8DATA_POSITION );
                        MSG_vidSaveMessage(u8Temp);
                        u8RxMessageState = SES_u8WAIT_WRITE_END;
                    }

                }
                else if (u8FrameReturn == COM_u8FRAME_ERROR)
                {
                    
                    u8SessionState = SES_u8SESSION_ERROR;
                    u8PrevSessionState = SES_u8SESSION_RX_MESSAGE;
                    u8RxMessageState = SES_u8RX_START;
                }

            }
            else if (u8RxMessageState == SES_u8BUFFER_READY)
            {
                au8PacketReceived[SES_nPACKET_NUMBER_INDEX] = u8Command + SES_u8CHAR_ZERO_OFFSET;
				u8Temp = SES_bSEND_STRING(au8PacketReceived);                    

            	if (u8Temp == TYP_u8TRUE)
            	{
                	MSG_bAddToMsgBuff(au8MessageTransferBuffer,u8DataLength);
                	BUF_vidFlush(pstrDataFrame);
                	u8RxMessageState = SES_u8RX_START;
				}
                
            
            }
            else if (u8RxMessageState ==  SES_u8WAIT_WRITE_END)
            {
                if (bWriteEnded == TYP_u8TRUE)
                {
                    u8Temp = SES_bSEND_STRING(au8MessageEnded);                    

            		if (u8Temp == TYP_u8TRUE)
            		{
                    	BUF_vidFlush(pstrDataFrame);
                    	u8SessionState = SES_u8SESSION_RUN;
                    	u8PrevSessionState = SES_u8SESSION_RX_MESSAGE;
                    	u8RxMessageState = SES_u8RX_START;
                    	bWriteEnded = TYP_u8FALSE;
					}
                }
            }
            else
            {
            
            }
            break;


        case SES_u8SESSION_SET_CURRENT:
                
            u8CurrentMessageDisplay = BUF_u8GetAtIndex(pstrDataFrame, SES_u8DATA_POSITION);
                
            u8Temp = SES_bSEND_STRING(au8CurrentMessageSet);                    

            if (u8Temp == TYP_u8TRUE)
            {
                BUF_vidFlush(pstrDataFrame);
            	u8SessionState      = SES_u8SESSION_RUN;
            	u8PrevSessionState  = SES_u8SESSION_SET_CURRENT; 
			}

        case SES_u8SESSION_ERROR:

            u8Temp = SES_bSEND_STRING(au8MessageError);                    

            if (u8Temp == TYP_u8TRUE)
            {
            	BUF_vidFlush(pstrDataFrame);
            	u8SessionState = SES_u8SESSION_IDLE;
			}
            break;
    }

}

void SES_vidSetWriteEnded(void)
{
    bWriteEnded = TYP_u8TRUE;
}

void SES_vidSetDataBuffer(BUF_tstrBuffer *pstrBuffer)
{

    pstrDataFrame = pstrBuffer;
}
/********************************/
/*	Local Functions' Definition */
/********************************/

